package com.org.redis.config;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 *
 * @author wang
 */
public class JedisUtil extends Redis{


    private static String ADDR_ARRAY = "134.175.117.176";

    private static int PORT = 7000;

    private static String AUTH =  null;

    private static int MAX_ACTIVE = 50;

    private static int MAX_IDLE = 10;

    private static int MAX_WAIT = 10 * 1000;

    private static int TIMEOUT = 10 * 1000;

    private static boolean TEST_ON_BORROW = true;

    private static JedisPool jedisPool = null;

    /**
     * 初始化Redis连接池
     */
    private static void initialPool() {
        try {

            JedisPoolConfig config = new JedisPoolConfig();

            config.setMaxTotal(MAX_ACTIVE);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT);
            config.setTestOnBorrow(TEST_ON_BORROW);
            config.setTestWhileIdle(true);
            config.setTestOnReturn(true);
            if (AUTH != null) {
                jedisPool = new JedisPool(config, ADDR_ARRAY.split(",")[0], PORT, TIMEOUT, AUTH);
            } else {
                jedisPool = new JedisPool(config, ADDR_ARRAY.split(",")[0], PORT, TIMEOUT);
            }

        } catch (Exception e) {
            e.printStackTrace();
            try {
                //如果第一个IP异常，则访问第二个IP
                JedisPoolConfig config = new JedisPoolConfig();
                config.setMaxTotal(MAX_ACTIVE);
                config.setMaxIdle(MAX_IDLE);
                config.setMaxWaitMillis(MAX_WAIT);
                config.setTestOnBorrow(TEST_ON_BORROW);
                jedisPool = new JedisPool(config, ADDR_ARRAY.split(",")[1], PORT, TIMEOUT, AUTH);
            } catch (Exception e2) {
                e.printStackTrace();
            }
        }
    }

    private static synchronized void poolInit() {
        if (jedisPool == null) {
            initialPool();
        }
    }

    /**
     * 同步获取Jedis实例
     *
     * @return Jedis
     */
    public synchronized static Jedis getJedis() {
        if (jedisPool == null) {
            poolInit();
        }

        Jedis jedis = null;
        try {
            if (jedisPool != null) {
                jedis = jedisPool.getResource();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(jedis);//归还到Redis池里面
        }
        return jedis;
    }

    /**
     * 释放jedis资源
     *
     * @param jedis
     */
    @SuppressWarnings("deprecation")
    public static void returnResource(final Jedis jedis) {
        if (jedis != null && jedisPool != null) {
            jedisPool.returnResource(jedis);
        }
    }

    @SuppressWarnings("deprecation")
    public static void returnResource2(final Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }


    /**
     * 关闭连接池
     */
    public static void closePool() {
        if (jedisPool != null) {
            jedisPool.close();
        }
    }

    /**
     * 设置 String
     *
     * @param key
     * @param value
     */
//    public synchronized static void setString(String key, String value) {
//        try {
//            value = TL.isEmpty(value) ? "" : value;
//            getJedis().set(key, value);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 设置 过期时间
     *
     * @param key
     * @param seconds 以秒为单位
     * @param value
     */
//    public synchronized static void setString(String key, int seconds, String value) {
//        try {
//            value = TL.isEmpty(value) ? "" : value;
//            getJedis().setex(key, seconds, value);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 获取String值
     *
     * @param key
     * @return value
     */
    public synchronized static String getString(String key) {
        if (getJedis() == null || !getJedis().exists(key)) {
            return null;
        }

        return getJedis().get(key);
    }

    public static void setHash(String keyName,String key,String value){
        getJedis().hset(keyName,key,value);
    }

    public static long getHLen(String keyName){
        return getJedis().hlen(keyName);
    }
}
